﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DontDestroy : MonoBehaviour {

    static protected Dictionary<string, GameObject> singletons = new Dictionary<string, GameObject> ();

    [SerializeField]
    protected bool singleton = false;
    
    [SerializeField]
    protected string singletonID = "static";

    protected virtual void Awake () {
        if (singleton) {
            if (singletons.ContainsKey (singletonID)) {
                if (singletons[singletonID] == gameObject) {

                } else {
                    GameObject.Destroy (gameObject);
                }
            } else {
                singletons.Add (singletonID, gameObject);
            }
        }
        DontDestroyOnLoad (gameObject);
    }
    protected virtual void OnDestroy () {
        if (singleton) {
            if (singletons.ContainsKey (singletonID)) {
                if (singletons[singletonID] == gameObject) {
                    singletons.Remove (singletonID);
                }
            }
        }
    }
}